﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VietThuongLMS.ViewModels;
using System.Web.Mvc;
using System.Data.Entity;
using VietThuongLMS.Utils;

/**
 * <Jason>
 * <Date: 20131114>
 * 
 */
namespace VietThuongLMS.Services
{

    public class DiemDanhService
    {
        public List<BangDiemDanh> GetDiemDanhList(int maLopHoc)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    List<BangDiemDanh> list = context.BangDiemDanhs.Where(m => m.MaHocVienLop == maLopHoc).ToList();
                    //List<DiemDanh> list = context.DiemDanhs.Include("LoaiTaiKhoan").Where(m => m.TaiKhoan.MaLoaiTaiKhoan == Utils.Constant.ACCOUNT_ROLE_TEACHER).ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("DiemDanhServices::GetDiemDanhList", ex.StackTrace, ex.Message);
                return null;
            }
        }

        public static DiemDanhViewModel GetDiemDanhList(int pageNum, int pageIndex, int maLopHoc)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    //List<BangDiemDanh> list = context.BangDiemDanhs.Where(m => m.HocVien_LopReference.Value.MaLopHoc == maLopHoc).OrderBy(m => m.HocVien_LopReference.Value.SoThanhVien).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                    //List<BangDiemDanh> list = context.BangDiemDanhs.Join.Where(m => m.HocVien_Lop.MaLopHoc == maLopHoc).OrderBy(m => m.HocVien_LopReference.Value.SoThanhVien).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();

                    List<BangDiemDanh> list = context.BangDiemDanhs.Join(context.HocVien_Lop,
                        bdd => bdd.MaHocVienLop,
                        hvl => hvl.ID,
                        (bdd, hvl) => hvl).Join(context.LopHocs,
                        hvl => hvl.MaLopHoc,
                        lh => maLopHoc,
                        (hvl, lh) => lh).Join(context.PhanCongs,
                        lh => lh.ID,
                        pc => pc.MaLopHoc,
                        (lh, pc) => pc).Join(context.BangDiemDanhs,
                        pc => pc.ID,
                        bdd => bdd.MaPhanCong,
                        (pc, bdd) => bdd).OrderBy(o => o.Ngay)
                        .Where(pc => pc.PhanCong.MaLopHoc == maLopHoc).Distinct().ToList();

                        //Where(hvl => hvl.MaLopHoc == maLopHoc).Join(context.PhanCongs,
                        
                        //.ToList();

                    foreach (BangDiemDanh bdd in list)
                    {
                        bdd.HocVien_LopReference.Load();
                        //bdd.HocVien_LopReference.Value.HocVienReference.Load();
                        bdd.HocVien_LopReference.Value.HocVienReference.Value.TaiKhoanReference.Load();
                        bdd.HocVien_LopReference.Value.LopHocReference.Load();
                        bdd.PhanCongReference.Load();
                        //bdd.PhanCongReference.Value.CaHocReference.Load() ;
                        //bdd.PhanCongReference.Load();
                    }

                    int total = list.Count();

                    return new DiemDanhViewModel(list, total);
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("DiemDanhServices::GetDiemDanhList", ex.StackTrace, ex.Message);
                return new DiemDanhViewModel();
            }
        }

        //public static bool InsertDiemDanh(DiemDanh comp)
        //{
        //    try
        //    {
        //        using (LMSDataEntities context = new LMSDataEntities())
        //        {
        //            context.AddToDiemDanhs(comp);
        //            context.SaveChanges();
        //            return true;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        DCS.ComLib.File.Log.Write("DiemDanhServices::InsertDiemDanh", ex.StackTrace, ex.Message);
        //        return false;
        //    }
        //}

        //public static bool DeleteDiemDanh(int id)
        //{
        //    try
        //    {
        //        using (LMSDataEntities context = new LMSDataEntities())
        //        {
        //            DiemDanh obj = context.DiemDanhs.Where(c => c.ID == id).FirstOrDefault();
        //            if (obj != null)
        //            {
        //                //obj. = Utils.Constant.GENERAL_STATUS_DELETED;
        //                context.DiemDanhs.DeleteObject(obj);
        //                context.SaveChanges();
        //                return true;
        //            }
        //            return false;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        DCS.ComLib.File.Log.Write("DiemDanhServices::DeleteDiemDanh", ex.StackTrace, ex.Message);
        //        return false;
        //    }
        //}

        public static BangDiemDanh GetDiemDanhById(int id, int maLopHoc)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    BangDiemDanh bdd = new BangDiemDanh();
                    //return context.BangDiemDanhs.Where(c => c.ID == id).FirstOrDefault();

                    bdd = context.BangDiemDanhs.Join(context.HocVien_Lop,
                                            bd => bd.MaHocVienLop,
                                            hvl => hvl.ID,
                                            (bd, hvl) => hvl).Join(context.LopHocs,
                                            hvl => hvl.MaLopHoc,
                                            lh => maLopHoc,
                                            (hvl, lh) => lh).Join(context.PhanCongs,
                                            lh => lh.ID,
                                            pc => pc.MaLopHoc,
                                            (lh, pc) => pc).Join(context.BangDiemDanhs,
                                            pc => pc.ID,
                                            bd => bd.MaPhanCong,
                                            (pc, bd) => bd).Where(bd => bd.ID == id).FirstOrDefault();

                    bdd.HocVien_LopReference.Load();
                    bdd.HocVien_LopReference.Value.HocVienReference.Value.TaiKhoanReference.Load();
                    bdd.HocVien_LopReference.Value.LopHocReference.Load();
                    bdd.PhanCongReference.Load();
                    //bdd.PhanCongReference.Value.CaHocReference.Load();
                    return bdd;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("DiemDanhServices::GetDiemDanhById", ex.StackTrace, ex.Message);
                return null;
            }
        }

        public static bool Edit(BangDiemDanh newModel, ref int id)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    BangDiemDanh model = context.BangDiemDanhs.Where(c => c.ID == newModel.ID).FirstOrDefault();
                    PhanCong pc = context.PhanCongs.Where(p => p.ID == model.MaPhanCong).FirstOrDefault();
                    id = pc.MaLopHoc.Value;
                    if (model != null)
                    {
                        model.VangMat = newModel.VangMat;
                        model.LyDo = newModel.LyDo;
                        //model.Ngay = pc.NgayDay;

                        context.SaveChanges();
                        return true;
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("DiemDanhServices::Edit", ex.StackTrace, ex.Message);
                return false;
            }
        }

        ////authenticate
        //public static DiemDanh GetDiemDanh(string tenDiemDanh)
        //{
        //    LMSDataEntities context = new LMSDataEntities();
        //    try
        //    {
        //        return context.DiemDanhs.Single(a => a.TenDiemDanh.Equals(tenDiemDanh));

        //    }
        //    catch (Exception ex)
        //    {
        //        DCS.ComLib.File.Log.Write("DiemDanhServices::GetDiemDanh", ex.StackTrace, ex.Message);
        //        return null;
        //    }
        //    finally
        //    {
        //        context.Dispose();
        //    }
        //}        

        /// <summary>
        /// NHDuc
        /// </summary>
        /// <param name="pageNum"></param>
        /// <param name="pageIndex"></param>
        /// <param name="maLopHoc"></param>
        /// <returns></returns>
        public static List<BangDiemDanhViewModel> GetDiemDanhByMaLopList(int maLopHoc, int MaHocVien)
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    List<BangDiemDanhViewModel> list = context.BangDiemDanhs
.Join(context.HocVien_Lop, bdd => bdd.MaHocVienLop, hvl => hvl.ID, (bdd, hvl) => new { BangDiemDanh = bdd, HocVien_Lop = hvl })
.Join(context.PhanCongs, bdd => bdd.BangDiemDanh.MaPhanCong, pc => pc.ID, (bdd, pc) => new { BangDiemDanh = bdd, HocVien_Lop = bdd.HocVien_Lop, PhanCong = pc })
//.Join(context.CaHocs, pc => pc.PhanCong.MaCaHoc, ca => ca.ID, (pc, ca) => new { BangDiemDanh = pc.BangDiemDanh, HocVien_Lop = pc.HocVien_Lop, PhanCong = pc.PhanCong, CaHoc = ca })
                        .Where(w => w.HocVien_Lop.MaLopHoc == maLopHoc && w.HocVien_Lop.MaHocVien == MaHocVien)
                        .Select(s => new BangDiemDanhViewModel
                            {
                                ID = s.BangDiemDanh.BangDiemDanh.ID,
                                Ngay = s.BangDiemDanh.BangDiemDanh.Ngay.Value,
                                VangMat = s.BangDiemDanh.BangDiemDanh.VangMat.Value,
                                LyDo = s.BangDiemDanh.BangDiemDanh.LyDo,
                                //CaHoc = s.CaHoc.ThoiGianBatDau + " - " + s.CaHoc.ThoiGianKetThuc
                                CaHoc = s.PhanCong.CaHoc
                            })
                        .OrderBy(o => o.Ngay).ToList();

                    return list;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("DiemDanhServices::GetDiemDanhByMaLopList", ex.StackTrace, ex.Message);
                return new List<BangDiemDanhViewModel>();
            }
        }
    }
}
